package org.iteam.xlz.manager.action.product;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.web.shiro.ShiroUtil;
import org.iteam.web.struts.action.AbstractModuleAction;
import org.iteam.web.struts.action.RestAction;
import org.iteam.xlz.core.entity.product.ProductSale;
import org.iteam.xlz.core.service.product.ProductSaleService;
import org.iteam.xlz.core.service.product.ProductService;
import org.iteam.xlz.core.service.product.ProductTypeService;
import org.iteam.xlz.core.service.ticket.TicketInfoService;

@SuppressWarnings("unchecked")
@Results({@Result(name="INDEX",type="freemarker",location="/WEB-INF/content/product/sale/index.html"),
    	  @Result(name="EDIT",type="freemarker",location="/WEB-INF/content/product/sale/edit.html"),
    	  @Result(name="GROUND",type="freemarker",location="/WEB-INF/content/product/sale/ground.html")})
public class ProductSaleAction extends AbstractModuleAction<ProductSale> implements RestAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3149746946102714968L;
	private final String PATH="/product/sale";
	
	@Override
	public ProductSale getModel() {
		if(null==model){
			model=new ProductSale();
		}
		return model;
	}
	@Override
	@Action(PATH+"/index")
	public String index() throws Exception {
		map.put("products",ProductService.getInstance().getActiveProduct());
		map.put("typeTree",ProductTypeService.getInstance().getTypeTree());
		return INDEX;
	}

	@Override
	@Action(PATH+"/query")
	public String query() {	
		map.put("total",ProductSaleService.getInstance().counts(_productname,ptid,parent,leftCreateTime,rightCreateTime));
		map.put("rows",ProductSaleService.getInstance().query(_productname,ptid,parent,leftCreateTime,rightCreateTime,page,rows));
		return MAP;
	}

	@Override
	public String show() {
		return null;
	}

	@Override
	@Action(PATH+"/create")
	public String create() {
		model.setCreator(ShiroUtil.currentAccount());
		boolean flag=ProductSaleService.getInstance().create(model,ticket);
		map.put("state",flag);
		if(!flag){
			map.put("info","操作失败，请查看是否绑定票卷");
		}
		return MAP;
	}

	@Override
	@Action(PATH+"/update")
	public String update() {
		boolean flag=ProductSaleService.getInstance().update(model);
		map.put("state",flag);
		return MAP;	
	}

	@Override
	@Action(PATH+"/validate")
	public String delete() {
		map.put("msg",ProductSaleService.getInstance().validate(model.getPpid(),model.getPid(),model.getProductuptime(),model.getProductdowntime()));
		return MAP;
	}

	@Override
	public String destroy() {
		return null;
	}

	@Override
	@Action(PATH+"/{_id}/edit")
	public String edit() throws Exception {
		map.put("products",ProductService.getInstance().getActiveProduct());
		ProductSale sale=ProductSaleService.getInstance().findOne(_id);
		super.object=sale;
		map.put("tickets",TicketInfoService.getInstance().getUnProductTicketOrAll(true));
		map.put("ticket",TicketInfoService.getInstance().findByPpid(sale.getPpid()));
		return EDIT;
	}

	@Override
	@Action(PATH+"/new")
	public String editNew() throws Exception {
		map.put("products",ProductService.getInstance().getActiveProduct());
		map.put("tickets",TicketInfoService.getInstance().getUnProductTicketOrAll(false));
		return "GROUND";
	}
	
	private String _productname;
	private String ptid;
	private String parent;
	private String leftCreateTime;
	private String rightCreateTime;
	private String ticket;

	public void set_productname(String _productname) {
		this._productname = _productname;
	}
	public void setPtid(String ptid) {
		this.ptid = ptid;
	}
	public void setParent(String parent) {
		this.parent = parent;
	}
	public void setLeftCreateTime(String leftCreateTime) {
		this.leftCreateTime = leftCreateTime;
	}
	public void setRightCreateTime(String rightCreateTime) {
		this.rightCreateTime = rightCreateTime;
	}
	public void setTicket(String ticket) {
		this.ticket = ticket;
	}
}
